@ Leture10_stack A Simple Calculator

	ldr r5, stack_bot_addr
	ldr r7, addr_str	@ r7 = 0x9000
	ldrb r6, [r7]		@ the 1st char
L1:
	ldr r8, =buffer
	cmp r6, #0		@ is it '\0'?
	beq ALL_DONE
	cmp r6, #0x20		@ if <space>, skip
	beq NEXT
	cmp r6, #0x2b		@ if '+'
	bne SUBTRACT
	ldr r4, [r5]		@ pop second operand
	sub r5, r5, #4
	ldr r3, [r5]		@ pop first operand
	add r3, r3, r4
	str r3, [r5]		@ push the result
	b NEXT
SUBTRACT:
	cmp r6, #0x2d		@ if '-'
	bne L2
	ldr r4, [r5]		@ pop second operand
	sub r5, r5, #4
	ldr r3, [r5]		@ pop first operand
	sub r3, r3, r4
	str r3, [r5]		@ push the result
	b NEXT
L2:
	strb r6, [r8]		@ put it to the buffer
	add r8, r8, #1
	add r7, r7, #1
	ldrb r6, [r7]		@ get next char
	cmp r6, #0x20		@ if <space>, done
	bne L2
	mov r6, #0		@ put '\0' at the end
	strb r6, [r8]
	ldr r0, =buffer
	bl asciiStr2Int
	add r5, r5, #4
	str r0, [r5]		@ push
NEXT:
	add r7, r7, #1
	ldrb r6, [r7]		@ get the next char
	b L1
ALL_DONE:
	@ the result is at the address
	@ contained in r5
	ldr r0, addr_screen
	ldr r1, [r5]
	bl printInt
	b halt

addr_screen:
	.word 0xd0000
addr_str:
	.word data_str
data_str:
	.ascii "3 4 + 5 -"
	.byte 0, 0, 0	@ (4byte alignment)
stack_bot_addr:
	.word 0xa000
buffer:
	.byte 0,0,0,0,0,0,0,0,0,0,0,0

asciiStr2Int:
	mov r12, r0
	eor r0, r0, r0
	ldr r11, [r12]
LOOP:
	cmp r11, #0
	beq DONE
	mov r10, #10
	mul r1, r0, r10
	sub r11, r11, #0x30
	add r0, r1, r11
	add r12, r12, #1
	ldrb r11, [r12]
	b LOOP
DONE:
	mov pc, lr